/**
 * Created by 123 on 2017/12/26.
 */
import {findMasterTask, taskEnable} from '../../services/master';
import {Toast} from 'antd-mobile'
import {Actions} from 'react-native-router-flux';

function showToast(msg) {
  Toast.fail(msg, 2)
}
export default {
  namespace: 'masterTaskList',

  state: {
    taskId:'',
    tabNum:0,
    page:1,
    pageSize:8,
    data: [],
    last:0,
    loading:true,
    dialogText:''
  },
  effects: {
    *query({payload}, {call, put, select}){
      yield put({type:'getLoading'})
      const {userId}= yield select(state => state.login);
      let current = '';
      let TabNum = '';
      const {page,pageSize,tabNum} = yield select(state => state.masterTaskList);
      if(payload === '' || payload === undefined){
          current = page;
          TabNum = tabNum;
      }else {
        if (payload.page ==='' || payload.page === undefined){
            current = page;
        }else {
            current = payload.page;
        }
          if (payload.tabNum ==='' || payload.tabNum === undefined){
              TabNum = tabNum;
          }else {
              TabNum = payload.tabNum;
          }
      }

      const {res, error}=yield call(findMasterTask, {pageNo: current, pageSize: pageSize, userId: userId,isEnable:TabNum});

      if (error) {
        yield put({type: 'cleanLoading'})
        // showToast(error)
        yield put({type:'thisDialog/thisInit'});
        yield put({type:'dialogShow',payload:error});
      } else {
        if (res.code == 1) {
          yield put({type: 'querySuccess', payload: {data:res.data,TabNum:TabNum}})
        } else {
          yield put({type: 'cleanLoading'})
          // showToast(res.message);
          yield put({type:'thisDialog/thisInit'});
          yield put({type:'dialogShow',payload:res.message});
        }
      }
    },
    *setEnable({payload}, {call, put, select}){
      const {res, error}=yield call(taskEnable, {id: payload});

      if (error) {
        console.log(error)
        // showToast(error)
        yield put({type:'thisDialog/thisInit'});
        yield put({type:'dialogShow',payload:error});
      } else {
        if (res.code == 1) {
          // showToast('启用成功！');
          yield put({type:'thisDialog/thisInit'});
          yield put({type:'dialogShow',payload:'派发成功！'});
        } else {
          // showToast(res.message);
          yield put({type:'thisDialog/thisInit'});
          yield put({type:'dialogShow',payload:'派发失败！'});
        }
      }
    },
  },
  reducers: {
    querySuccess(state, {payload}){
      let list = [];
      let num = payload.data.pageNo;
      let last = payload.data.last;
      payload.data.list.map((val, index) => (
        list.push({
          id: val.id,
          prjName: val.prjName,
          taskNum: val.taskNum,
          isEnable: val.isEnable,
          taskName: val.name,
          date:val.createDate.length>10?val.createDate.substring(0,10):val.createDate,
        })
      ));
      return {...state, data: list,page:num,last:last,loading:false,tabNum:payload.TabNum}
    },
    resetData(state, {payload}){
      return {...state, data: []};
    },
    getLoading(state,{payload}){
        return{...state,loading:true}
    },
    cleanLoading(state,{payload}){
        return{...state,loading:false}
    },
    getClean(state,{payload}){
      return {...state,page:1,loading:true,dialogText:''}
    },
    setTaskId(state,{payload}){
      return {...state,taskId:payload}
    },
    dialogShow(state,{payload}){
      return {...state,dialogText:payload}
    }
  }
}
